package spring.cloud.tasks.client.item_task_processor.task_items_processor;

import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.client.item_task_processor.task_items_processor.task_data_processor.MultipleTaskDataProcessor;
import spring.cloud.tasks.client.item_task_processor.task_items_processor.task_data_processor.SingleTaskDataProcessor;

@Slf4j
public class SleepWhenNoDataModeTaskProcessingThread extends Thread {
    private SleepWhenNoDataModeTaskItemsProcessor taskProcessor;

    public SleepWhenNoDataModeTaskProcessingThread(SleepWhenNoDataModeTaskItemsProcessor sleepWhenNoDataModeTaskProcessor) {
        this.taskProcessor = sleepWhenNoDataModeTaskProcessor;
    }

    public void run() {
        long startTime = 0;
        long sequence = 0;
        Object taskDataId = null;//可能多个也可能一个task
        while (true) {
            try {
                if (taskProcessor.destroy == true) {
                    synchronized (taskProcessor.threadList) {
                        taskProcessor.threadList.remove(Thread.currentThread());
                    }
                    return;
                }

                if (!taskProcessor.multipleTaskDataProcessor) {
                    taskDataId = taskProcessor.getTaskDataId();
                } else {
                    taskDataId = taskProcessor.getTaskDataIds();
                }
                if (taskDataId == null) {
                    taskProcessor.getTaskDataIdsFromDb();
                    continue;
                }

                try {
                    taskProcessor.runningTaskDataIdList.add(taskDataId);
                    startTime = taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis();
                    sequence = sequence + 1;
                    if (!taskProcessor.multipleTaskDataProcessor) {//SingleTaskDataProcessor
                        if (((SingleTaskDataProcessor<Object>) taskProcessor.taskDataProcessor)//
                                .execute(taskDataId)) {
                            taskProcessor.addSuccessNum(1, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                        } else {
                            taskProcessor.addFailNum(1, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                        }
                    } else {//MultipleTaskDataProcessor
                        if (((MultipleTaskDataProcessor<Object>) taskProcessor.taskDataProcessor)//
                                .execute(taskDataId)) {
                            taskProcessor.addSuccessNum(((Object[]) taskDataId).length, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                        } else {
                            taskProcessor.addFailNum(((Object[]) taskDataId).length, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                        }
                    }
                } catch (Throwable ex) {
                    if (taskProcessor.multipleTaskDataProcessor == false) {
                        taskProcessor.addFailNum(1, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                    } else {
                        taskProcessor.addFailNum(((Object[]) taskDataId).length, taskProcessor.ruleTaskProcessor.zooKeeperDataManager.zooKeeperCurrentTimeMillis() - startTime);
                    }
                    log.error("TaskDataId :" + taskDataId + " ", ex);
                } finally {
                    taskProcessor.runningTaskDataIdList.remove(taskDataId);
                }
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }
    }
}
